package com.lsfb.duishangbaos.Utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Base64;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 作者:nonsense
 * 時間:2016/5/6
 * 作用:
 * e_mail:bdflower@vip.qq.com
 */
public class ZipPic {

//    public static void GetSize(String filePath) {
//        BitmapFactory.Options options = new BitmapFactory.Options();
//        options.inJustDecodeBounds = true;
//        BitmapFactory.decodeFile(filePath, options);
//        int height = options.outHeight;
//        int width = options.outWidth;
//        int inSampleSize = 1;
//        int reqHeight = 800;
//        int reqWidth = 480;
//        if (height > reqHeight || width > reqWidth) {
//            final int heightRatio = Math.round((float) height / (float) reqHeight);
//            final int widthRatio = Math.round((float) width / (float) reqWidth);
//            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
//        }
//    }
//    public  static void getBitmap(   BitmapFactory.Options options ){
//        options.inSampleSize = calculateInSampleSize(options, 480, 800);
//        options.inJustDecodeBounds = false;
//        Bitmap bitmap= BitmapFactory.decodeFile(filePath, options);
//
//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//        bm.compress(Bitmap.CompressFormat.JPEG, 60, baos);
//        byte[] b = baos.toByteArray();
//    }
    //计算图片的缩放值
    public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height/ (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

    /**.
     *
     * 根据路径压缩图片并返回bitmap用于显示
     * @param filePath
     * @return
     */
    public static Bitmap getSmallBitmap(String filePath) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, 480, 800);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(filePath, options);
    }

    //把bitmap转换成String
    public static String bitmapToString(String filePath) {

        Bitmap bm = getSmallBitmap(filePath);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 40, baos);
        byte[] b = baos.toByteArray();
        return Base64.encodeToString(b, Base64.DEFAULT);
    }

    public static File zippng(String filepath) {
        BitmapFactory.Options options=new BitmapFactory.Options();
        options.inJustDecodeBounds=true;
        Bitmap bitmap=BitmapFactory.decodeFile(filepath,options);
        options.inJustDecodeBounds=false;
        int be=(int)(options.outHeight/ (float)200);
        if (be<=0) {
            be=1;
        }
        options.inSampleSize=be;
        bitmap=BitmapFactory.decodeFile(filepath, options);
//		int w=bitmap.getWidth();
//		int h=bitmap.getHeight();
        File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"cysj"+System.currentTimeMillis()+".jpg");
        if (file.exists()) {
            file.delete();
        }
        try {
            FileOutputStream outputStream=new FileOutputStream(file);
            if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)) {
                try {
                    outputStream.flush();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return null;
                }
                try {
                    outputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return null;
                }

            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
        return file;
    }
}
